import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;

public class JDBCLibrary {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("666666");

        Connection connection= dataSource.getConnection();
        System.out.println(connection);
        Scanner scanner=new Scanner(System.in);
        System.out.println("学号");
        int id=scanner.nextInt();
        System.out.println("年龄");
        int age=scanner.nextInt();
        //第一
//        String sql1="update jdbc3 set number=66 where age=40;";
//        String sql2="update jdbc3 set number=66 where age=41;";
//        String sql3="insert into jdbc3 values(55,10);";
//        Statement statement= connection.createStatement();
//        statement.executeUpdate(sql1);
//        statement.executeUpdate(sql2);
//        statement.executeUpdate(sql3);
       String sql1="create table  IF NOT EXISTS  jdbc3(number int,age int);";
        String sql2 ="insert into jdbc3 values(?,?);";
        String sql3="";

        //第二
//        PreparedStatement preparedStatement1=connection.prepareStatement(sql1);
//        preparedStatement1.executeUpdate();
//        preparedStatement1=connection.prepareStatement(sql2);
//        preparedStatement1.setInt(1,id);
//        preparedStatement1.setInt(2,age);
//        preparedStatement1.addBatch();
//        preparedStatement1.setInt(1,id+1);
//        preparedStatement1.setInt(2,age+1);
//        preparedStatement1.addBatch();
//        preparedStatement1.executeBatch();

        int bookId= scanner.nextInt();
        String bookNane=scanner.next();
       String sql4="DROP TABLE IF EXISTS category;";
       String sql5="CREATE TABLE category (" +
             " id int(11) PRIMARY KEY AUTO_INCREMENT,\n" +
                " name varchar(20)\n" +
                ");";
        String sql6="insert into category values(?,?);";

        PreparedStatement preparedStatement4=connection.prepareStatement(sql4);
        preparedStatement4.executeUpdate();
        preparedStatement4=connection.prepareStatement(sql5);
        preparedStatement4.executeUpdate();
        preparedStatement4=connection.prepareStatement(sql6);
        preparedStatement4.setInt(1,bookId);
        preparedStatement4.setString(2,bookNane);
        preparedStatement4.executeUpdate();



        preparedStatement4.close();

        connection.close();

    }
}
